package com.bayair.doctor.net;

import com.bayair.doctor.net.config.HttpConfig;
import com.bayair.doctor.net.services.Api;
import com.bayair.doctor.net.services.ExpandApiService;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;


public class RxManager {

    private Api mApiService;
    private ExpandApiService mExpandApiService;

    private Map<String, CompositeDisposable> map;

    private volatile static RxManager ourInstance;

    private RxManager() {
        setupRestServices();
        map = new HashMap<>();
    }

    public static RxManager getInstance() {
        if (ourInstance == null) {
            synchronized (RxManager.class) {
                if (ourInstance == null) {
                    ourInstance = new RxManager();
                }
            }
        }
        return ourInstance;
    }

    private <T> T createRestQueue(Class<T> restClass,
                                  String endpoint,
                                  Collection<Interceptor> interceptors,
                                  boolean followRedirects) {
        OkHttpClient okHttpClient = RestServiceUtils.buildOkHttpClient(followRedirects, interceptors);

        return new Retrofit.Builder()
                .baseUrl(endpoint)
                .client(okHttpClient)
                .addConverterFactory(ScalarsConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.createWithScheduler(Schedulers.io()))
                .build()
                .create(restClass);
    }

    private void setupRestServices() {
        setupApiRestService();
        setupExpandApiRestService();
        setupOtherService();
    }

    public Api getApiService() {
        return mApiService;
    }

    public ExpandApiService getExpandApiService() {
        return mExpandApiService;
    }

    private void setupApiRestService() {
        String endpoint = HttpConfig.BASE_URL;
        mApiService = createRestQueue(Api.class, endpoint, RestServiceUtils.getApiInterceptors(), false);
    }

    //不是baseurl标准的
    private void setupExpandApiRestService() {
        String endpoint = HttpConfig.BASE_TEST;
        mExpandApiService = createRestQueue(ExpandApiService.class, endpoint, null, false);

    }

    private void setupOtherService() {

    }

    //记录tag方便后面取消请求
    public void add(String key, Disposable disposable) {
        Set<String> keySet = map.keySet();
        if (keySet.contains(key)) {
            CompositeDisposable compositeDisposable = map.get(key);
            compositeDisposable.add(disposable);
        } else {
            CompositeDisposable compositeDisposable = new CompositeDisposable();
            compositeDisposable.add(disposable);
            map.put(key, compositeDisposable);
        }
    }

    //根据tag取消请求
    public void clear(String key) {
        Set<String> keySet = map.keySet();
        if (keySet.contains(key)) {
            CompositeDisposable compositeDisposable = map.get(key);
            compositeDisposable.clear();
            map.remove(key);
        }
    }
}
